<html><head><meta charset= "utf-8" ></head>
    <body>
    <div class="preview__inner-2 markdown_views prism-atom-one-light" style="padding: 10px 20px 430px;"><div class="cl-preview-section"><p align="center"><font size="6"><strong>线程测验</strong></font></p><p>
</p></div><div class="cl-preview-section"><p><font size="4">１.写出实现简单线程池的伪代码，包括线程池的数据结构，初始化操作，添加任务操作，销毁操作。
</font></p>
</div><div class="cl-preview-section"><p><font size="4">２.哲学家就餐问题
</font></p>
</div><div class="cl-preview-section"><blockquote>
<p>哲学家就餐问题:五个哲学家共用一张圆桌，分别坐在周围的五张椅子上，在圆桌上有五个碗和五只筷子，他们的生活方式是交替的进行思考和进餐。平时，一个哲学家进行思考，饥饿时便试图取用其左右最靠近他的筷子，只有在他拿到两只筷子时才能进餐。进餐完毕，放下筷子继续思考。</p>
</blockquote>
</div><div class="cl-preview-section"><p>
设计一个程序五个哲学家共用一张圆桌，分别坐在周围的五张椅子上，在圆桌上有五个碗和五只筷子，他们的生活方式是交替的进行思考和进餐。平时，一个哲学家进行思考，饥饿时便试图取用其左右最靠近他的筷子，只有在他拿到两只筷子时才能进餐。进餐完毕，放下筷子继续思考。下面为此问题的一种代码实现，请根据自己的理解写出代码中的思路，以注释的形式加在每步。
</p>
</div><div class="cl-preview-section"><pre><code class="prism language-c"><span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;memory.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;pthread.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;errno.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;math.h&gt;</span></span>
pthread_mutex_t chopstick<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span> <span class="token punctuation">;</span>
<span class="token keyword">void</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">*</span>left<span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">*</span>right<span class="token punctuation">,</span><span class="token keyword">char</span> phi<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>phi<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">'A'</span><span class="token punctuation">:</span>
            <span class="token operator">*</span>left <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
            <span class="token operator">*</span>right <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">'B'</span><span class="token punctuation">:</span>
            <span class="token operator">*</span>left <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token operator">*</span>right <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">'C'</span><span class="token punctuation">:</span>
            <span class="token operator">*</span>left <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
            <span class="token operator">*</span>right <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">'D'</span><span class="token punctuation">:</span>
            <span class="token operator">*</span>left <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
            <span class="token operator">*</span>right <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">'E'</span><span class="token punctuation">:</span>
            <span class="token operator">*</span>left <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
            <span class="token operator">*</span>right <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>   
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token operator">*</span><span class="token function">eat_think</span><span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span>arg<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">char</span> phi <span class="token operator">=</span> <span class="token operator">*</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span>arg<span class="token punctuation">;</span>
    <span class="token keyword">int</span> left<span class="token punctuation">,</span>right<span class="token punctuation">;</span> 
    <span class="token function">get</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>left<span class="token punctuation">,</span><span class="token operator">&amp;</span>right<span class="token punctuation">,</span>phi<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">;</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
        <span class="token function">pthread_mutex_lock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Philosopher %c fetches chopstick %d\n"</span><span class="token punctuation">,</span> phi<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">pthread_mutex_trylock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> EBUSY<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">pthread_mutex_unlock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Philosopher %c fetches chopstick %d\n"</span><span class="token punctuation">,</span> phi<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Philosopher %c is eating.\n"</span><span class="token punctuation">,</span>phi<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">usleep</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">pthread_mutex_unlock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>left<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Philosopher %c release chopstick %d\n"</span><span class="token punctuation">,</span> phi<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">pthread_mutex_unlock</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>right<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Philosopher %c release chopstick %d\n"</span><span class="token punctuation">,</span> phi<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">;</span>
 
    <span class="token punctuation">}</span>   
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    pthread_t A<span class="token punctuation">,</span>B<span class="token punctuation">,</span>C<span class="token punctuation">,</span>D<span class="token punctuation">,</span>E<span class="token punctuation">;</span> <span class="token comment">//5个哲学家</span>
    <span class="token keyword">int</span> i<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token function">pthread_mutex_init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>chopstick<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>A<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span> eat_think<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token string">"A"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>B<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span> eat_think<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token string">"B"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>C<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span> eat_think<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token string">"C"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>D<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span> eat_think<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token string">"D"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>E<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">,</span> eat_think<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">void</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token string">"E"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
    <span class="token function">pthread_join</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_join</span><span class="token punctuation">(</span>B<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_join</span><span class="token punctuation">(</span>C<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_join</span><span class="token punctuation">(</span>D<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">pthread_join</span><span class="token punctuation">(</span>E<span class="token punctuation">,</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p><font size="4">
3.编程实现生产者消费者模型，生产者的主要作用是生成一定数量的数据放到缓冲区中，然后重复此过程。与此同时，消费者也在缓冲区消耗着这些数据。</font> </p>
<p> <font size="4"> 要求：保证生产者不会在缓冲区满时加入数据，消费者也不会在缓冲区中空时消耗数据 。                                                                </font></p>    
 <p><font size="4"> 提示：缓冲区可以设置为全局数组,生产者和消费者均为一个线程,利用线程间同步机制实现。</font></p>
</div></div>
<body>
</html>
